package com.ruoyi.paas.service.impl;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.paas.domain.XspUserSiginLog;
import com.ruoyi.paas.mapper.XspUserSiginLogMapper;
import com.ruoyi.xsp.domain.XspUser;
import com.ruoyi.xsp.mapper.XspUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.paas.mapper.XspUserSignMapper;
import com.ruoyi.paas.domain.XspUserSign;
import com.ruoyi.paas.service.IXspUserSignService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户签到Service业务层处理
 * 
 * @author zhiminhu
 * @date 2021-04-26
 */
@Service
public class XspUserSignServiceImpl implements IXspUserSignService 
{
    @Autowired
    private XspUserSignMapper xspUserSignMapper;
    @Autowired
    private XspUserMapper xspUserMapper;
    @Autowired
    private XspUserSiginLogMapper xspUserSiginLogMapper;
    /**
     * 查询用户签到
     * 
     * @param id 用户签到ID
     * @return 用户签到
     */
    @Override
    public XspUserSign selectXspUserSignById(Long id)
    {
        return xspUserSignMapper.selectXspUserSignById(id);
    }
    /**
     * 查询用户签到
     *
     * @param userCode 用户签到ID
     * @return 用户签到
     */
    @Override
    public XspUserSign selectXspUserSignByUserCode(Long userCode){
        return xspUserSignMapper.selectXspUserSignByUserCode(userCode);
    }
    /**
     * 查询用户签到列表
     * 
     * @param xspUserSign 用户签到
     * @return 用户签到
     */
    @Override
    public List<XspUserSign> selectXspUserSignList(XspUserSign xspUserSign)
    {
        return xspUserSignMapper.selectXspUserSignList(xspUserSign);
    }

    /**
     * 新增用户签到
     * 
     * @param xspUserSign 用户签到
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int insertXspUserSign(XspUserSign xspUserSign)
    {
        xspUserSign.setCreateTime(DateUtils.getNowDate());
        insertLog(xspUserSign);
        return xspUserSignMapper.insertXspUserSign(xspUserSign);
    }
    private void insertLog(XspUserSign xspUserSign){
        XspUserSiginLog log=new XspUserSiginLog();
        log.setUserCode(xspUserSign.getUserCode());
        log.setNickname(xspUserSign.getNickname());
        log.setSignCount(xspUserSign.getSignCount());
        int reward=10;
        if(xspUserSign.getSignCount()>1){
            reward=xspUserSign.getSignCount()+reward;
            log.setReward("连续"+xspUserSign.getSignCount()+"天签到，获得 "+reward+" 积分。");
        }else {
            log.setReward("签到成功获得 "+reward+" 积分。");
        }
        log.setCreateBy("");
        log.setCreateTime(new Date());
        log.setUpdateTime(new Date());
        xspUserSiginLogMapper.insertXspUserSiginLog(log);
        XspUser user=xspUserMapper.selectXspUserByUserCode(xspUserSign.getUserCode());
        //user.set 增加积分
        xspUserMapper.updateXspUser(user);
    }
    /**
     * 修改用户签到
     * 
     * @param xspUserSign 用户签到
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int updateXspUserSign(XspUserSign xspUserSign)
    {
        xspUserSign.setUpdateTime(DateUtils.getNowDate());
        insertLog(xspUserSign);
        return xspUserSignMapper.updateXspUserSign(xspUserSign);
    }

    /**
     * 删除用户签到对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteXspUserSignByIds(String ids)
    {
        return xspUserSignMapper.deleteXspUserSignByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户签到信息
     * 
     * @param id 用户签到ID
     * @return 结果
     */
    @Override
    public int deleteXspUserSignById(Long id)
    {
        return xspUserSignMapper.deleteXspUserSignById(id);
    }
}
